import allure
from selenium.webdriver.common.by import By

from base.BackgroundBase.back_base import BasePage, BaseHandle


class LoginPage(BasePage):
    def __init__(self):
        super().__init__()
        self.username = By.NAME, "username"
        self.password = By.NAME, "password"
        self.code = By.NAME, "vertify"
        self.submit = By.NAME, "submit"

    def find_username(self):
        return self.get_element(self.username)

    def find_password(self):
        return self.get_element(self.password)

    def find_code(self):
        return self.get_element(self.code)

    def find_login_btn(self):
        return self.get_element(self.submit)


class LoginHandle(BaseHandle):
    def __init__(self):
        self.login_page = LoginPage()

    @allure.step(title="输入用户名")
    def input_username(self, username):
        self.input_text(self.login_page.find_username(), username)

    @allure.step(title="输入密码")
    def input_password(self, password):
        self.input_text(self.login_page.find_password(), password)

    @allure.step(title="验证码")
    def input_code(self, code):
        self.input_text(self.login_page.find_code(), code)

    @allure.step(title="点击登录")
    def click_login_btn(self):
        self.login_page.find_login_btn().click()


class LoginProxy:
    def __init__(self):
        self.login_handle = LoginHandle()

    def go_mall_home_page(self, username, password, code):
        self.login_handle.input_username(username)
        self.login_handle.input_password(password)
        self.login_handle.input_code(code)
        self.login_handle.click_login_btn()
